﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pasteles.DataBase;
using Pasteles.Controler;

namespace Pasteles
{
    public partial class nhapSPquay : Form
    {
        Connection cn = new Connection("config.xml");
        DataTable dt_banh;
        DataView dv_banh1;
        DataView dv_banh2;
        DataView dv_banh3;
        DataView dv_banh4;
        DataTable sp_taiquay;
        StringProcessing objStringProcessor = new StringProcessing();
        string maBanh;
        public nhapSPquay()
        {
            InitializeComponent();
            this.load_Form();
            DataGridViewButtonColumn btn_xoa = new DataGridViewButtonColumn();
            gv_sptaiquay.Columns.Add(btn_xoa);
            btn_xoa.HeaderText = "Xóa";
            btn_xoa.Text = "Xóa";
            btn_xoa.Name = "btn_xoa";
            btn_xoa.UseColumnTextForButtonValue = true;
            gv_sptaiquay.Columns[3].Width = 150;
            gv_sptaiquay.Columns[3].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
        }
        private void load_Form()
        {
            dt_banh = cn.excecuteQueryToDataTable("select * from banh");
            dv_banh1 = new DataView(dt_banh);
            dv_banh1.RowFilter = "MA_LOAI_BANH='1'";
            lb_banh1.DataSource = dv_banh1;
            lb_banh1.DisplayMember = "Ten_Banh";
            lb_banh1.ValueMember = "Ma_Banh";

            dv_banh2 = new DataView(dt_banh);
            dv_banh2.RowFilter = "MA_LOAI_BANH='2'";
            lb_banh2.DataSource= dv_banh2;
            lb_banh2.DisplayMember = "Ten_Banh";
            lb_banh2.ValueMember = "Ma_Banh";

            dv_banh3 = new DataView(dt_banh);
            dv_banh3.RowFilter = "MA_LOAI_BANH='3'";
            lb_banh3.DataSource = dv_banh3;
            lb_banh3.DisplayMember = "Ten_Banh";
            lb_banh3.ValueMember = "Ma_Banh";

            dv_banh4 = new DataView(dt_banh);
            dv_banh4.RowFilter = "MA_LOAI_BANH='4'";
            lb_banh4.DataSource = dv_banh4;
            lb_banh4.DisplayMember = "Ten_Banh";
            lb_banh4.ValueMember = "Ma_Banh";

            sp_taiquay = cn.excecuteQueryToDataTable("select TEN_BANH,SO_LUONG_TON_QUAY,GIA_KINH_DOANH,NGAY_LEN_QUAY from banh inner join san_pham_tai_quay on banh.MA_BANH = san_pham_tai_quay.MA_BANH order by NGAY_LEN_QUAY DESC;");
            sp_taiquay.Columns["TEN_BANH"].ColumnName = "Tên Bánh";
            sp_taiquay.Columns["SO_LUONG_TON_QUAY"].ColumnName = "Số Lượng";
            sp_taiquay.Columns["GIA_KINH_DOANH"].ColumnName = "Giá Bán";
            sp_taiquay.Columns["NGAY_LEN_QUAY"].ColumnName = "Ngày Lên Quầy";

            gv_sptaiquay.DataSource = sp_taiquay;

        
            txt_SoLuong.Text = "";

        }

        private void nhapSPquay_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void btn_nhapspbanh_Click(object sender, EventArgs e)
        {
            bool flag = true;
           
            try
            {
                if (txt_SoLuong.Text != "")
                {
                    foreach (DataRow dtRows in sp_taiquay.Rows)
                    {
                        if (lbl_tenbanh.Text == dtRows[0].ToString())
                        {
                            MessageBox.Show("Đã Nhập Bánh Này Rồi");
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        cn.excecuteQuery("call them_san_pham_tai_quay(1,2," + maBanh.ToString() + "," + txt_SoLuong.Text.ToString().Trim() + ",0,now(),'" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "');");
                        load_Form();
                    }
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Số Lượng hoặc Giá Tiền");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            lbl_tenbanh.Text = "";

        }

        private void btn_sua_Click(object sender, EventArgs e)
        {

        }

        private void lb_banh1_DoubleClick(object sender, EventArgs e)
        {
            if (lb_banh1.SelectedIndex != -1)
            {
                lbl_tenbanh.Visible = true;
                maBanh = lb_banh1.SelectedValue.ToString();
                lbl_tenbanh.Text = lb_banh1.Text;
            }
        }

        private void lb_banh2_DoubleClick(object sender, EventArgs e)
        {
            if (lb_banh2.SelectedIndex != -1)
            {
                lbl_tenbanh.Visible = true;
                maBanh = lb_banh2.SelectedValue.ToString();
                lbl_tenbanh.Text = lb_banh2.Text;
            }
        }

        private void lb_banh3_DoubleClick(object sender, EventArgs e)
        {
            if (lb_banh3.SelectedIndex != -1)
            {
                lbl_tenbanh.Visible = true;
                maBanh = lb_banh3.SelectedValue.ToString();
                lbl_tenbanh.Text = lb_banh3.Text;
            }
        }

        private void lb_banh4_DoubleClick(object sender, EventArgs e)
        {
            if (lb_banh4.SelectedIndex != -1)
            {
                lbl_tenbanh.Visible = true;
                maBanh = lb_banh4.SelectedValue.ToString();
                lbl_tenbanh.Text = lb_banh3.Text;
            }
        }

        private void gv_sptaiquay_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gv_sptaiquay.Columns[e.ColumnIndex].Name == "btn_xoa")
            {
                try
                {
                    //MessageBox.Show(DateTime.Parse(gv_sptaiquay.Rows[e.RowIndex].Cells[4].Value.ToString()).ToString("yyyy-MM-dd hh:mm:ss ").Trim());
                    cn.excecuteQuery("call xoa_san_pham_tai_quay(1,'" + DateTime.Parse(gv_sptaiquay.Rows[e.RowIndex].Cells[4].Value.ToString()).ToString("yyyy-MM-dd HH:mm:ss").Trim() + "',2);");
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.ToString());
                }
                load_Form();
               // MessageBox.Show(DateTime.Parse(gv_sptaiquay.Rows[e.RowIndex].Cells[4].Value.ToString()).ToString("yyyy-MM-dd hh:mm:ss"));
            }
        }

      

        private void txt_SoLuong_TextChanged(object sender, EventArgs e)
        {

        }
        private void txt_SoLuong_KeyDown(object sender, KeyEventArgs e)
        {
            this.validateNum(e);
        }
        private void txt_SoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
            {
                btn_nhapspbanh_Click(sender, e);
            }
        }

        //private void txt_GiaTien_TextChanged(object sender, EventArgs e)
        //{
        //    if (txt_GiaTien.Text.ToString().Length > 3)
        //    {
        //        try
        //        {
        //            objStringProcessor.cutNo(txt_GiaTien);
        //        }
        //        catch (Exception ex)
        //        {
        //        }
        //    }
        //}
        private void txt_GiaTien_KeyDown(object sender, KeyEventArgs e)
        {
            this.validateNum(e);
        }
        private void txt_GiaTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
            {
                btn_nhapspbanh_Click(sender, e);
            }
        }


          //------------------------------------------------------------------------------------Validate-----------------------------------------------------------------------------------//
        private bool nonNumberEntered = false;//bien flag dung de khoa ban phim nhap lieu
        private void validateNum(KeyEventArgs e)//Ham chi cho phep nhap so nguyen duong
        {
            nonNumberEntered = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    if (e.KeyCode != Keys.Back)
                    {

                        nonNumberEntered = true;
                    }
                }
            }
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonNumberEntered = true;
            }
        }

   
       

       



    }
}
